import javax.swing.UIManager;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.net.URLClassLoader;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

/**
 * GFrame is the application frame.
 * It creates splash screen, menu bar, tool bar and 
 * scrollable GPanel object on the left side and
 * both scrollable help panel and scrollable message panel on the right side.
 *
 * @version 1.0
 * @author P.Jaya Krishna
 */
public class GFrame extends JFrame
{
	// The following objects may be referred throughout the project to change
	// the look and contents of the frame
	
	// application's main menu
	GMenuBar menuBar;
	// application's main menu handler
	MenuHandler menuHandler;
	// contentPane of the frame
	JPanel contentPane;
	// application's main tool bar
	ToolBar toolBar;
	// application's status bar
	GStatusBar statusBar;
	// for showing help
	JTextArea helpPanel;
	// for adding scrolling functionality to help area
	JScrollPane helpScrollPane;
	// for showing any other messages
	JTextArea messagePanel;
	// for adding scrolling functionality to message area
	JScrollPane messageScrollPane;
	// for working with graphs
	GPanel gPanel;
	// for adding scrolling functionality
	JScrollPane scrollPane;
	// for splitting the main area
	JSplitPane mainSplitPane;
	// for splitting the right sub area
	JSplitPane subSplitPane;
	int msgWidth, pWidth;

	
	/**
	 * Creates GFrame object with all the necessary initializations.
	 */
	public GFrame()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//URLClassLoader cl = (URLClassLoader)getClass().getClassLoader();
		setIconImage(Toolkit.getDefaultToolkit().getImage("images/grasp.png"));
		setTitle("GRASP - [File Not Saved]");
		
		// menubar
		menuHandler = new MenuHandler(this);
		menuBar = new GMenuBar(menuHandler);
		setJMenuBar(menuBar);
		
		// contentPane for the frame
		contentPane = new JPanel();
		contentPane.setOpaque(true);
		contentPane.setLayout(new BorderLayout());
		
		// tool bar
		toolBar = new ToolBar(this);
		
		// status bar
		statusBar = new GStatusBar();
		
		// help panel
		helpPanel = new JTextArea();
		helpPanel.setTabSize(4);
		helpPanel.setEditable(false);
		
		// making help panel scrollable
		helpScrollPane = new JScrollPane(helpPanel);
		
		// message panel		
		messagePanel = new JTextArea();
		messagePanel.setTabSize(4);		
		messagePanel.setEditable(false);

		// making message panel scrollable
		messageScrollPane = new JScrollPane(messagePanel);
		messageScrollPane.setPreferredSize(new Dimension(50,150));		
		
		// graph editing panel
		gPanel = new GPanel(this, null);
		gPanel.setPreferredSize(new Dimension(1200, 1000));
		
		// making gPanel scrollable
		scrollPane = new JScrollPane(gPanel);
		scrollPane.setPreferredSize(new Dimension(600,450));
		scrollPane.getHorizontalScrollBar().setUnitIncrement(15);
		scrollPane.getVerticalScrollBar().setUnitIncrement(15);
		
		// for splitting right side area
		subSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, helpScrollPane, messageScrollPane);
		//subSplitPane.setDividerLocation(240);
		//subSplitPane.setMinimumSize(new Dimension(0,0));
		subSplitPane.setOneTouchExpandable(true);
		subSplitPane.addAncestorListener(new AncestorListener()
		{
			public void ancestorMoved( AncestorEvent event )
         	{
				pWidth = scrollPane.getBounds().width;
           		msgWidth = subSplitPane.getBounds().width;
         	}
         
         	public void ancestorAdded( AncestorEvent event )
	     	{    	 }

   			public void ancestorRemoved( AncestorEvent event )
   			{    	}
    	});
		
		// for splitting the main area
		mainSplitPane	= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane, subSplitPane);
		mainSplitPane.setOneTouchExpandable(true);
		
		contentPane.add(toolBar, BorderLayout.NORTH);
		contentPane.add(mainSplitPane, BorderLayout.CENTER);
		contentPane.add(statusBar, BorderLayout.SOUTH);
		setContentPane(contentPane);
		addComponentListener(new ComponentAdapter()
			{
				public void componentResized(ComponentEvent ce)
				{
					double ratio = (double) pWidth/(pWidth+msgWidth);
					mainSplitPane.setDividerLocation(ratio);
					subSplitPane.setDividerLocation(0.5);
				}
			});
		
		pack();
		setVisible(true);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
	}
}